//----------------------------------
//---Heavy Pulse Rifle SWEP---------
//-----------Made by Brian Nevec----
//----------------------------------
//---Part of the SMOD Weapons Pack--
//----------------------------------

//if on server
if SERVER then

	//add the file
	AddCSLuaFile("shared.lua");

	//set the hold type
	SWEP.HoldType			= "smg";

end

//if on client
if CLIENT then

	//set some stuff
	SWEP.PrintName			= "Heavy Pulse Rifle";
	SWEP.Author				= "Brian Nevec";
	SWEP.Slot				= 2;
	SWEP.SlotPos			= 3;
	SWEP.Purpose			= "Jacked from a drop ship";
	SWEP.Instructions		= "Left Click: Fire";

	//set the category
	SWEP.Category			= "SMOD";

	//set the selection icon
	SWEP.WepSelectIcon = surface.GetTextureID("weapons/pulseicon");

	-- Kill Icon
	if(file.Exists("../materials/weapons/pulse_killicon.vmt")) then

		killicon.Add("weapon_heavy_pulse","weapons/pulse_killicon",Color(255,255,255));

	end 

end

//is it spawnable?
SWEP.Spawnable				= true;
SWEP.AdminSpawnable			= false;

//models
SWEP.ViewModel				= "models/weapons/v_smg1.mdl";
SWEP.WorldModel				= "models/weapons/w_smg1.mdl";

//more stuff
SWEP.Weight					= 25;
SWEP.AutoSwitchTo			= false;
SWEP.AutoSwitchFrom			= false;

//primary settings
SWEP.Primary =
{
	ClipSize				= 200,
	DefaultClip				= -1,
	Automatic				= true,
	Ammo					= "ar2"
}

//secondary settings
SWEP.Secondary =
{
	ClipSize				= -1,
	DefaultClip				= -1,
	Automatic				= true,
	Ammo					= "none"
}

//some stuff
SWEP.NextAmmoAdd			= 0;
SWEP.FireSound				= nil;

//sound
local FireSound				= Sound("npc/combine_gunship/gunship_fire_loop1.wav");

//on initialize
function SWEP:Initialize()

	//if on server
	if SERVER then

		//set the hold type
		self:SetWeaponHoldType(self.HoldType);

	elseif CLIENT then

		//else, create a font
		surface.CreateFont("coolvetica",100,500,true,false,"Heavy_Pule_Rifle_HUD");

	end

	//create the fire sound
	self.FireSound = CreateSound(self.Weapon,FireSound);

	//set the clip
	self.Weapon:SetClip1(200);

end

//when we want to attack with the primary
function SWEP:PrimaryAttack()

	//if we can
	if self.Weapon:CanPrimaryAttack() then

		//create a bullet
		local Bullet = {};
		Bullet.Src = self.Owner:GetShootPos();
		Bullet.Attacker = self.Owner;
		Bullet.Dir = self.Owner:GetAimVector();
		Bullet.Spread = Vector(0.02,0.02,0);
		Bullet.Num = 1;
		Bullet.Damage = 3;
		Bullet.Force = 10;
		Bullet.Tracer = 1;
		Bullet.TracerName = "AirboatGunHeavyTracer";
		
		//fire it
		self.Weapon:FireBullets(Bullet);
		self.Weapon:ShootEffects();

		//take off some ammo
		self.Weapon:TakePrimaryAmmo(1);

	end

	//delay the fire
	self.Weapon:SetNextPrimaryFire(CurTime() + 0.07);

	//return false
	return true;

end

//when we think
function SWEP:Think()

	//if we don't have any keys pressed
	if !self.Owner:KeyDown(IN_ATTACK) && !self.Owner:KeyDown(IN_ATTACK2) then

		//if the delay is off
		if CurTime() > self.NextAmmoAdd then

			//give ammo
			self.Weapon:SetClip1(math.Clamp(self.Weapon:Clip1() + 1,0,200))

			//set the delay
			self.NextAmmoAdd = CurTime() + 0.09;

		end

	end

	//when pressed fire
	if self.Owner:KeyPressed(IN_ATTACK) && self.Weapon:Clip1() > 1 then

		//play sound
		self.FireSound:Play();

	end

	//when released fire
	if self.Owner:KeyReleased(IN_ATTACK) then

		//stop sound
		self.FireSound:Stop();

	end

	//when out of ammo
	if self.Weapon:Clip1() < 1 then

		//stop sound
		self.FireSound:Stop();

	end

end

//when reloading
function SWEP:Reload()

	//stop
	return false;

end

//when holstering
function SWEP:Holster()

	//sound off
	self.FireSound:Stop();

	//return
	return true;

end

//when restoring
function SWEP:OnRestore()

	//sound off
	self.FireSound:Stop();

end

//when we want to draw a hud
function SWEP:HUDShouldDraw(E)

	//if it's the ammo display
	if E == "CHudAmmo" then

		//remove it
		return false;

	end

	//allow everything else
	return true;

end

//when we deploy
function SWEP:Deploy()

	//run the deploy animation
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW);

	//and return
	return true;

end

if( !CLIENT ) then return; end

SWEP.BeamMaterial = Material( "cable/redlaser" );

function SWEP:ViewModelDrawn( )

	local vm = self.Owner:GetViewModel( );
	local id = vm:LookupAttachment( "muzzle" );
	local at = vm:GetAttachment( id );

	local pos = at.Pos;
	local ang = at.Ang;
	ang:RotateAroundAxis( ang:Right( ), 90 );
	
	local text = "AMMO: "..self.Weapon:Clip1( );
	
	surface.SetFont( "default" );
	local w = ( surface.GetTextSize( text ) );
	
	pos = pos + ang:Forward( ) * ( w + 4 ) * -.25;

	cam.Start3D2D( pos, ang, .2 );
		
		surface.SetTextColor( 0, 0, 0, 255 );
		surface.SetTextPos( 2, 2 );
		surface.DrawText( text );
		
		surface.SetTextColor( 255,255, 255, 255 );
		surface.SetTextPos( 1, 1 );
		surface.DrawText( text );
	
	cam.End3D2D( );
	
	/*
	cam.Start3D( EyePos( ), EyeAngles( ) );
		
		//cam.IgnoreZ( true );
			
			//render.SetMaterial( Material( "effects/select_ring" ) );
			//render.DrawSprite( at.Pos, 8, 8, color_white );
			
		//cam.IgnoreZ( false );
		
	
	
	self:DrawModel( );
	
	local origin = at.Pos;
	local endpos = self.Owner:GetEyeTrace( ).HitPos;
	
	local length = 16;
	local segments = math.floor( origin:Distance( endpos ) / length / 3 );
	local spread = 2;
	
	render.SetMaterial( self.BeamMaterial );
	
	render.StartBeam( segments + 1 );
		
		for i = 0, segments do
			
			local target_pos = origin + ( ( endpos - origin ) * ( i / segments ) );
			target_pos = target_pos + VectorRand( ):GetNormal( ) * math.random( spread );
			
			render.AddBeam( target_pos, 32, 0, color_white );
			
		end
		
		render.AddBeam( endpos, 32, 0, color_white );
		
	render.EndBeam( );
	
	//cam.End3D( );
	*/
end